<?php

class UserController extends Controller
{

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules()
	{
		return array(
	            array('allow',
	                'actions'=>array('index','logout','list','del','create','Password'),
	                'users'=>array('@'),
	            ),
	            array(
	            	'allow',
	            	'actions'=>array('login'),
	            	'users'=>array('*')
	            ),
	            array('deny',  // deny all users
					'users'=>array('*'),
				),
			);
	}


	public $navTitle='用户管理';

	public function actionIndex()
	{
		$this->pageTitle="用户信息";
		$user= User::model()->find("username=:username",array(":username"=>Yii::app()->user->name));
		if(isset($_POST['User']))
		{
			$user->attributes =$_POST['User'];
			if($user->validate())
			{
				$user->save();
				Yii::app()->user->setFlash('success', "修改成功!");  
			}
		}
		$this->render("index",array("model"=>$user));
	}

	public function actionPassword()
	{
		$this->pageTitle="密码修改";
		$model  = new PassWordForm;
		if(isset($_POST['PassWordForm']))
		{
			$model->attributes =$_POST['PassWordForm'];
			if($model->validate())
			{
				$user= User::model()->find("username=:username",array(":username"=>Yii::app()->user->name));
				$user->password =$user->HashPassWord($model->password);
				$user->save();
				Yii::app()->user->setFlash('success', "修改成功!");  
				$this->refresh();
			}
		}
		$this->render('password',array('model'=>$model));
	}
	

	public  function actionlogin()
	{
		if (!Yii::app()->user->isGuest) {
			$this->redirect($this->createUrl("site/index"));
		}
		$this->layout=".";
		$model = new LoginForm;
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect($this->createUrl("site/index"));
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	public function actionlogout()
	{
		Yii::app()->user->logout();
		$this->redirect($this->createUrl("user/login"));
	}

	public function actionList($page=0)
	{
		$this->pageTitle='账户管理';
		$data = new CActiveDataProvider("User",array(
			'pagination'=>array(
				'pageSize'=>Yii::app()->params["pagesize"])
			));
		$this->render('list',array('data'=>$data));
	}

	public function actionDel($id)
	{
		User::model()->deleteByPk($id);
		$this->redirect($this->createUrl('user/list'));
	}

	public function actionCreate()
	{
		$model = new User;
		if (isset($_POST["User"])) {
			$model->attributes=$_POST['User'];
			if($model->validate())
			{
				$model->save();
				Yii::app()->user->setFlash('success', "添加成功!");  
				$this->refresh();
			}
		}
		$this->render('create',array('model'=>$model));
	}

}